﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ZEBuz.Questionnaire.Lib.Util;

namespace ZEBuz.Questionnaire.Lib.Entities
{
    public enum MeasureType
    {
        Average,
        TotalScore
    }
    [Serializable]
    public class XMLQuestionnaire
    {
        public int count { get; set; }
        public string name { get; set; }
        public bool enablePrev { get; set; }
        public bool enableNext { get; set; }
        public MeasureType measureType { get; set; }
        public List<XMLQuestion> questions { get; set; }
        // 暂时不支持一个问题对应一个选项组，仅支持一个问题一个选项组
        public List<XMLOption> options { get; set; }
        public List<XMLQuestion.XMLQuestionCategory> categories { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlName">如OLSON</param>
        /// <returns></returns>
        public static XMLQuestionnaire GetEntityFromXML(string xmlName)
        {
            XmlDocument doc = new XmlDocument();
            string path = Const.DirFormPath + xmlName + ".xml";
            doc.Load(path);
            XmlNode xmlqn = doc.SelectSingleNode("/questionnaire");
            XmlNodeList xmlquestions = doc.SelectNodes("/questionnaire/questioninfo/questions/question");
            XmlNodeList xmlquestionCategories = doc.SelectNodes("/questionnaire/questioninfo/categories/category");
            XmlNodeList xmloptvalues = doc.SelectNodes("/questionnaire/questioninfo/optvalues/optvalue");
            XmlNodeList xmloptions = xmlqn.SelectNodes("/questionnaire/optioninfo/optionset/option");

            //questionnaire info
            XMLQuestionnaire qnDTO = new XMLQuestionnaire
             {
                 count = xmlquestions.Count,
                 name = xmlqn.Attributes["name"].Value,
                 enablePrev = (xmlqn.Attributes["enablePrev"].Value == "1"),
                 enableNext = (xmlqn.Attributes["enableNext"].Value == "1"),
                 measureType = xmlqn.Attributes["measureType"].Value.ToEnum<MeasureType>(),
                 questions = new List<XMLQuestion>(),
                 options = new List<XMLOption>(),
                 categories = new List<XMLQuestion.XMLQuestionCategory>()
             };

            //questions info
            foreach (XmlNode q in xmlquestions)
            {
                string qid = q.Attributes["id"].Value;
                string content = q.ChildNodes[0].InnerText;

                if (qnDTO.questions.Find(o => o.id.Equals(qid)) == null)
                {
                    XMLQuestion qs = new XMLQuestion
                    {
                        id = qid,
                        content = content,
                        category = null,
                        opsvalues = new int[] { 5, 4, 3, 2, 1 }
                    };

                    qnDTO.questions.Add(qs);
                }
            }

            //option info
            foreach (XmlNode o in xmloptions)
            {
                qnDTO.options.Add(new XMLOption
                {
                    id = o.Attributes["id"].Value,
                    content = o.ChildNodes[0].InnerText
                });
            }

            #region to change question info
            //option info
            foreach (XmlNode ov in xmloptvalues)
            {
                string val = ov.Attributes["value"].Value;
                //notice:不做XML端的VALUE为空的校验
                string[] questionids = ov.Attributes["questionid"].Value.Split(',');
                foreach (string qid in questionids)
                {
                    var q = qnDTO.questions.Find(o => o.id == qid);
                    if (q != null)
                    {
                        q.opsvalues = new int[] { 5, 4, 3, 2, 1 };
                    }
                }
            }

            //category info
            foreach (XmlNode qc in xmlquestionCategories)
            {
                string[] questionids = qc.Attributes["questionid"].Value.Split(',');
                XmlNodeList xmlcategorylevels = qc.SelectNodes("levels/level");
                List<XMLQuestion.XMLQuestionCategory.XMLQuestionCategoryLevel> levels
                    = new List<XMLQuestion.XMLQuestionCategory.XMLQuestionCategoryLevel>();

                foreach (XmlNode l in xmlcategorylevels)
                {
                    levels.Add(new XMLQuestion.XMLQuestionCategory.XMLQuestionCategoryLevel
                    {
                        min = l.Attributes["min"].Value.ToInt(),
                        max = l.Attributes["max"].Value.ToInt(),
                        name = l.Attributes["name"].Value,
                        text = l.Attributes["text"].Value
                    });
                }

                foreach (string qid in questionids)
                {
                    var q = qnDTO.questions.Find(o => o.id == qid);
                    if (q != null)
                    {
                        q.category = new XMLQuestion.XMLQuestionCategory
                        {
                            id = qc.Attributes["id"].Value,
                            name = qc.Attributes["name"].Value,
                            desc = qc.Attributes["desc"].Value,
                            levels = levels,
                            questionIds = qc.Attributes["questionid"].Value
                        };

                        //加到CATEGORY列表属性
                        if (qnDTO.categories.Find(o => o.id == q.category.id) == null)
                        {
                            qnDTO.categories.Add(q.category);
                        }
                    }
                }
            }
            #endregion

            return qnDTO;
        }
    }
}
